package com.darkplanet.management.dao;

import com.darkplanet.management.domain.Task;
import com.darkplanet.management.util.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import java.sql.SQLException;

import java.util.List;

public class TaskDaoImp implements ITaskDao {
   private QueryRunner qr = new QueryRunner(JDBCUtil.getDs());
    @Override
    public Task getTaskById(Integer task_id) {
        String sql = "select * from task where task_id = ?";
        Task task = null;
        try {
             task = qr.query(sql, new BeanHandler<Task>(Task.class), task_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return task;
    }

    @Override
    public List<Task> getTasks() {
        String sql = "select * from task ";
        List<Task> tasks = null;
        try {
            tasks = qr.query(sql, new BeanListHandler<Task>(Task.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return tasks;
    }

    @Override
    public void addTask(Integer fun_id, String task_tittle, String start_time, String finish_time, Integer employee_id, String priority, String complex_desc) {
        String sql = "insert into task (fun_id,task_tittle,start_time,finish_time,employee_id,priority,complex_desc)  values (?,?,?,?,?,?,?)";
        try {
            qr.update(sql,fun_id,task_tittle,start_time,finish_time,employee_id,priority,complex_desc);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteTask(Integer task_id) {
        String sql = "delete from task where task_id = ?";
        try {
            qr.update(sql,task_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateTask(Integer fun_id, String task_tittle, String start_time, String finish_time, Integer employee_id, String priority, String complex_desc,Integer task_id) {
        String sql = "update task set fun_id = ? , task_tittle = ? , start_time = ? ,finish_time = ?,employee_id = ?,priority = ?,complex_desc = ? where task_id = ?";
        try {
            qr.update(sql,fun_id,task_tittle,start_time,finish_time,employee_id,priority,complex_desc,task_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

